package com.wdd.atcrowdfunding.potal.controller;

import com.wdd.atcrowdfunding.bean.Cert;
import com.wdd.atcrowdfunding.bean.Member;
import com.wdd.atcrowdfunding.bean.MemberCert;
import com.wdd.atcrowdfunding.manager.service.CertService;
import com.wdd.atcrowdfunding.potal.service.MemberCertService;
import com.wdd.atcrowdfunding.potal.service.MemberService;
import com.wdd.atcrowdfunding.util.AjaxResult;
import com.wdd.atcrowdfunding.util.Const;
import com.wdd.atcrowdfunding.util.CpachaUtil;
import com.wdd.atcrowdfunding.util.MailUtil;
import com.wdd.atcrowdfunding.vo.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private CertService certService;
    @Autowired
    private MemberCertService memberCertService;

    @RequestMapping("/uploadCert")
    public String uploadCert(){
        return "member/apply-1";
    }

    @RequestMapping("/basicinfo")
    public String basicinfo(){
        return "member/basicinfo";
    }

    @RequestMapping("/accttype")
    public String accttype(){
        return "member/accttype";
    }

    @RequestMapping("/toEmail")
    public String toEmail(){
        return "member/apply-2";
    }

    @RequestMapping("/writeEmail")
    public String writeEmail(){
        return "member/apply-3";
    }

    /**
     * 更新账户类型
     * @param session
     * @param accttype
     * @return
     */
    @RequestMapping("/updateAcctType")
    @ResponseBody
    public AjaxResult updateAcctType(HttpSession session,String accttype){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            //获取登录会员信息
            Member loginMember = (Member) session.getAttribute(Const.LOGIN_MEMBER);
            loginMember.setAccttype(accttype);
            //跟新账户类型
            memberService.updateAcctType(loginMember);
            ajaxResult.setSuccess(true);
        }catch( Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    /**
     * 用户基本信息填写 ： 真是姓名 身份证号 电话号码
     * @param session
     * @param member
     * @return
     */
    @RequestMapping("/updateBasicinfo")
    @ResponseBody
    public AjaxResult updateBasicinfo(HttpSession session,Member member){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            //获取登录会员信息
            Member loginMember = (Member) session.getAttribute(Const.LOGIN_MEMBER);
            loginMember.setRealname(member.getRealname());
            loginMember.setTelephone(member.getTelephone());
            loginMember.setCardnum(member.getCardnum());
            //跟新账户类型
            memberService.updateBasicinfo(loginMember);
            ajaxResult.setSuccess(true);
        }catch( Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    /**
     * 文件保存
     * @param session
     * @param ds
     * @return
     */
    @RequestMapping("/doUploadCert")
    @ResponseBody
    public AjaxResult doUploadCert(HttpSession session, Data ds){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            //获取登录会员信息
            Member loginMember = (Member) session.getAttribute(Const.LOGIN_MEMBER);

            //保存会员与资质关系数据

            String realPath = session.getServletContext().getRealPath("/pics");

            List<MemberCert> certimgs = ds.getCertimgs();
            for (MemberCert memberCert : certimgs){
                //资质文件上传
                MultipartFile fileImg = memberCert.getFileImg();
                String extName = fileImg.getOriginalFilename().substring(fileImg.getOriginalFilename().lastIndexOf("."));
                String uuidName = UUID.randomUUID().toString();
                String filename = realPath + "/cert" + "/" + uuidName + extName;
                fileImg.transferTo(new File(filename));

                memberCert.setIconpath(uuidName + extName);
                memberCert.setMemberid(loginMember.getId());
            }

            memberCertService.saveMemberCert(certimgs);


            ajaxResult.setSuccess(true);
        }catch( Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    /**
     * 填写邮箱并发送验证码
     * @param session
     * @param email
     * @return
     */
    @RequestMapping("/startProcess")
    @ResponseBody
    public AjaxResult startProcess(HttpSession session,String email){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            //获取登录会员信息
            Member loginMember = (Member) session.getAttribute(Const.LOGIN_MEMBER);

            //如果用户输入新的邮箱 替换旧的
            if (!loginMember.getEmail().equals(email)){
                loginMember.setEmail(email);
                memberService.updateEmail(loginMember);
            }
            //发送验证码
            session.setAttribute(Const.CODE,"");
            CpachaUtil cpachaUtil = new CpachaUtil();
            String code = cpachaUtil.generatorVCode();
            session.setAttribute(Const.CODE,code);
            MailUtil.sendMail(email,"验证码:"+code,"验证码");


            ajaxResult.setSuccess(true);
        }catch( Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    /**
     * 验证验证码
     * @param session
     * @param authcode
     * @return
     */
    @RequestMapping("/finishApply")
    @ResponseBody
    public AjaxResult finishApply(HttpSession session,String authcode){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            //获取登录会员信息
            Member loginMember = (Member) session.getAttribute(Const.LOGIN_MEMBER);

            String code = (String) session.getAttribute(Const.CODE);
            if(!code.equalsIgnoreCase(authcode)){
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("验证码填写错误");
                return ajaxResult;
            }
            //更改状态
            loginMember.setAuthstatus("1");
            memberService.updateAuthstatus(loginMember);

            session.setAttribute(Const.CODE,"");
            ajaxResult.setSuccess(true);
        }catch( Exception e){
            e.printStackTrace();
            ajaxResult.setMessage("审核错误");
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    /**
     * 重新发送验证码
     * @param session
     * @return
     */
    @RequestMapping("/reloadEmail")
    @ResponseBody
    public AjaxResult reloadEmail(HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            //获取登录会员信息
            Member loginMember = (Member) session.getAttribute(Const.LOGIN_MEMBER);

            //发送验证码
            session.setAttribute(Const.CODE,"");
            CpachaUtil cpachaUtil = new CpachaUtil();
            String code = cpachaUtil.generatorVCode();
            session.setAttribute(Const.CODE,code);
            MailUtil.sendMail(loginMember.getEmail(),"验证码:"+code,"验证码");


            ajaxResult.setSuccess(true);
        }catch( Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }


    /**
     * 回显  真是姓名 身份证号 电话号码
     */
    @RequestMapping("/showCertType")
    @ResponseBody
    public AjaxResult showCertType(HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            //获取登录会员信息
            Member loginMember = (Member) session.getAttribute(Const.LOGIN_MEMBER);
            List<Cert> certList = certService.queryCertByAccttype(loginMember.getAccttype());
            //session.setAttribute("certList",certList);
            ajaxResult.setData(certList);
            ajaxResult.setSuccess(true);
        }catch( Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

}
